class MerchantsController < ApplicationController
  #before_filter :require_city
  before_filter :require_login,:only => [:edit,:update,:destroy]
  include UsersHelper
  include ApplicationHelper
  include EnumsHelper::ProductType
  # GET /merchants
  # GET /merchants.json
  def index
    @merchants = Merchant.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @merchants }
    end
  end

  def landing

  end
  
  def dashboard

  end
  # GET /merchants/1
  # GET /merchants/1.json
  def show
    @merchant = Merchant.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @merchant }
    end
  end

  # GET /merchants/new
  # GET /merchants/new.json
  def new

  end
  
  # POST /merchants
  # POST /merchants.json
  def create
    @merchant = Merchant.new(params[:merchant])
    @user = User.new(params[:user])
    @user.email = @merchant.email
    if @user.save
      create_user_context(@user)
      @merchant.user = @user
      if @merchant.save
        userrole = UserRole.new
        userrole.user = @user
        userrole.role = 1
        userrole.save
        add_merchant_to_user_context(@merchant)
        redirect_to :action => 'dashboard', :controller => 'merchants', :status => 303 
        return
      else
        format.html { render action: "new" }
        format.json { render json: @merchant.errors, status: :unprocessable_entity }
     end
    end
  end
  
  def info_create
  end
  
  def info_edit
    @merchant = get_current_merchant()
    @merchant_address = MerchantAddress.where(:merchant_id=>@merchant.id)[0]
  end

  def info_update
    @merchant = get_current_merchant()
    @merchant_address = MerchantAddress.find(params[:merchant_address_id])
    @merchant_address.merchant = @merchant
    @merchant_info = MerchantInfo.find(params[:merchant_info_id])
    @merchant_info.merchant = @merchant
    @merchant_address.update_attributes(params[:merchant_address])
    @merchant_info.update_attributes(params[:merchant_info])
    redirect_to :action => 'info', :controller => 'merchants', :id => @merchant_info.merchant.id, :status => 303
  end
  
  def info_submit
    @merchant_info = MerchantInfo.new(params[:merchant_info])
    @merchant_info.merchant = get_current_merchant()
    @merchant_address = MerchantAddress.new(params[:merchant_address])
    @merchant_address.merchant = get_current_merchant()
    if(!params[:display_pic].nil?)
      orig_filename =  params[:display_pic].original_filename
      filename = SecureRandom.uuid + "_" + sanitize_filename(orig_filename)
      AWS::S3::S3Object.store(filename, params[:display_pic].read, MERCHANT_DISPLAY_IMAGE_BUCKET, :access => :public_read)
      url = AWS::S3::S3Object.url_for(filename, MERCHANT_DISPLAY_IMAGE_BUCKET, :authenticated => false)
      @merchant_info.display_image = url
    end
    if @merchant_info.save
      @merchant_address.save
      redirect_to :action => 'info', :controller => 'merchants', :id => @merchant_info.merchant.id, :status => 303    
    else
      redirect_to :action => 'info_create', :controller => 'merchants', :status => 303    
    end
  end
  
  def add_address
  end
  
  def submit_address
    @merchant = Merchant.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @merchant }
    end
  end
  
  # GET /merchants/1/edit
  def edit
    @merchant = Merchant.find(params[:id])
  end


  # PUT /merchants/1
  # PUT /merchants/1.json
  def update
    @merchant = Merchant.find(params[:id])

    respond_to do |format|
      if @merchant.update_attributes(params[:merchant])
        format.html { redirect_to @merchant, notice: 'Merchant was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @merchant.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /merchants/1
  # DELETE /merchants/1.json
  def destroy
    @merchant = Merchant.find(params[:id])
    @merchant.destroy

    respond_to do |format|
      format.html { redirect_to merchants_url }
      format.json { head :no_content }
    end
  end
  
  def gift_cards
    @products = Product.where(:product_type=>EnumsHelper::ProductType::GIFT_CARD,:merchant_id=>params[:id])
  end
  
  def info
    @merchant = Merchant.find(params[:id])
    @giftcards = Product.where(:product_type=>EnumsHelper::ProductType::GIFT_CARD,:merchant_id=>params[:id])
    @products = Product.where(:product_type=>EnumsHelper::ProductType::PRODUCT,:merchant_id=>params[:id])
  end
  
  def products
    @products = Product.where(:product_type=>EnumsHelper::ProductType::PRODUCT,:merchant_id=>params[:id])
  end
  
end
